package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.GoodsDetails.Buy;
import com.banzhi.shop.entity.OrderAffirm.OrderAffirm;
import com.banzhi.shop.model.CallBack.OrderAffirmCallBack;
import com.banzhi.shop.model.IOrderAffirmModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/19.
 */

public class ShareOrderAffirmImpl implements IOrderAffirmModel {
    @Override
    public void zhifu(final String goods_id, String total, String address_id, String optionid, final String remarks, final OrderAffirmCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.BuyselfShopUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("goods_id", goods_id);
        params.addBodyParameter("total", total);
        params.addBodyParameter("address_id", address_id);
        if (optionid != null && !"".equals(optionid)) {
            params.addBodyParameter("optionid", optionid);
        }
        if (remarks != null && !"".equals(remarks)) {
            params.addBodyParameter("remarks", remarks);
        }
        LogUtils.e("OrderAffirmImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        OrderAffirm affirm = gson.fromJson(result,OrderAffirm.class);
                        callBack.onzhifuSuccess(affirm);
                    } else {
                        callBack.onzhifuError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("OrderAffirmImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(21)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void buy(String goods_id, String total, String optionid, String addressid, final OrderAffirmCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.goodsPreCreateUrl);
        params.addBodyParameter("goods_id", goods_id);
        params.addBodyParameter("total", total);
        if (optionid != null) {
            params.addBodyParameter("optionid", optionid);
        }
        if (optionid != null) {
            params.addBodyParameter("addressid", addressid);

        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("buy", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Buy buy = gson.fromJson(result, Buy.class);
                        callBack.onbuySuccess(buy.getRes());
                    } else/* if(jsonobject.optString("code").equals("1"))*/{
                        callBack.onbuyError(jsonobject);
                    }/*else if(jsonobject.optString("code").equals("2")){
                        callBack.onAddAddressSuccess(jsonobject);
                    }*/
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("buy", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
